using System;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Toenda.TFL.Base;
using Toenda.Rails.Data;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;

public partial class Controls_Main_ctrl_account : UserControlBaseClass {
	private AccountProvider ap = new AccountProvider();
	private StaticProvider sp = new StaticProvider();
	private ServerPath serverpath = new ServerPath();

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		if(!IsPostBack) {
			//if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
			//Account acc = ap.GetAccountBySession(Session["Zappr"].ToString());
			Account acc = ap.GetAccount(Session["ZapprUserId"].ToString());

			// first slash
			Label litSlashOne = new Label();
			litSlashOne.CssClass = "txtSmallGreen";
			litSlashOne.Text = "/";

			userText.Controls.Add(litSlashOne);

			// link dashboard
			HyperLink linkDash = new HyperLink();
			linkDash.CssClass = "pathWayLink";
			linkDash.Text = acc.Username;
			linkDash.NavigateUrl = "~/images/" + acc.Username + "/default.aspx";

			userText.Controls.Add(linkDash);

			// second slash
			Label litSlashTwo = new Label();
			litSlashTwo.CssClass = "txtSmallGreen";
			litSlashTwo.Text = "/";

			userText.Controls.Add(litSlashTwo);

			// link user
			Label linkUser = new Label();
			linkUser.CssClass = "txtH2";
			linkUser.Text = GetLocalResourceObject("Settings").ToString();

			userText.Controls.Add(linkUser);

			this.btnDeleteMe.OnClientClick = "javascript:chk=confirm('"
				+ GetLocalResourceObject("DeleteThis").ToString()
				+ "');return chk;";

			//
			// LOAD SETTINGS
			//

			// account type
			//if(acc.Type == 0) {
			//    this.lblAccType.Text = GetLocalResourceObject("FreeAccount").ToString() + " | ";
			//}
			//else {
			//    this.lblAccType.Text = GetLocalResourceObject("ProAccount").ToString();
			//    //this.linkUpgrade.Visible = false;
			//}

			// imagelist layout
			this.ddlImagelistLayout.SelectedValue = Convert.ToString((int)acc.AccountSettings.ImageListLayout);
			//}
			//else {
			//    Response.Redirect("~/login.aspx");
			//}
		}
	}

	/// <summary>
	/// btnSave_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnSave_Click(object sender, EventArgs e) {
		//if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
		//    Account acc = ap.GetAccountBySession(Session["Zappr"].ToString());
		Account acc = ap.GetAccount(Session["ZapprUserId"].ToString());
		AccountSettings accs = acc.AccountSettings;

		accs.ImageListLayout = (ImageListLayout)Convert.ToInt32(this.ddlImagelistLayout.SelectedValue);

		try {
			ap.SaveAccountSettings(accs);

			//Response.Redirect("~/default.aspx?user=" + acc.Username);
			Response.Redirect("~/images/" + acc.Username + "/default.aspx");
		}
		catch(Exception ex) {
			LiteralControl litError = new LiteralControl();
			litError.Text = "<div class=\"errorBox\">";
			litError.Text += "<span class=\"txtH3\">" + GetLocalResourceObject("Error").ToString() + "</span>";
			litError.Text += "<br /><br />";
			litError.Text += "<span class=\"txtNormal\">" + ex.Message + "</span>";
			litError.Text += "</div>";
			litError.Text += "<br /><br />";

			//errLogin.Controls.Add(new LiteralControl("<br /><br />"));
			errLogin.Controls.Add(litError);
		}
		//}
		//else {
		//    Response.Redirect("~/login.aspx");
		//}
	}

	/// <summary>
	/// btnDeleteMe_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnDeleteMe_Click(object sender, EventArgs e) {
		//if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
		ImageProvider ip = new ImageProvider();
		//Account acc = ap.GetAccountBySession(Session["Zappr"].ToString());
		Account acc = ap.GetAccount(Session["ZapprUserId"].ToString());

		if(acc.Username != "admin") {
			List<ImageItem> list = ip.GetImageList(acc.ID, 0, 0, 0, true, true);

			foreach(ImageItem item in list) {
				FileInfo info = new FileInfo(
					serverpath.Path + @"\Images\" + item.ImageOriginal
				);
				info.Delete();

				info = new FileInfo(
					serverpath.Path + @"\Images\" + item.Image1024
				);
				info.Delete();

				info = new FileInfo(
					serverpath.Path + @"\Images\" + item.Image500
				);
				info.Delete();

				info = new FileInfo(
					serverpath.Path + @"\Images\" + item.Image240
				);
				info.Delete();

				info = new FileInfo(
					serverpath.Path + @"\Images\" + item.Image100
				);
				info.Delete();

				info = new FileInfo(
					serverpath.Path + @"\Images\" + item.Image75x75
				);
				info.Delete();

				ip.DeleteImage(item.ID);
			}

			ap.DeleteUser(acc.ID);

			if(Session.Count > 0) {
				ap.Logout(Session["Zappr"].ToString());
				Session.RemoveAll();
			}

			Response.Redirect("~/finishaccountclosing/default.aspx");
		}
		//}
		//else {
		//    Response.Redirect("~/login.aspx");
		//}
	}

	/// <summary>
	/// btnCancel_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnCancel_Click(object sender, EventArgs e) {
		//if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
		//Account acc = ap.GetAccountBySession(Session["Zappr"].ToString());
		Account acc = ap.GetAccount(Session["ZapprUserId"].ToString());
		Response.Redirect("~/images/" + acc.Username + "/default.aspx");
		//}
		//else {
		//    Response.Redirect("~/login.aspx");
		//}
	}
}
